<?php

use yii\bootstrap\BootstrapPluginAsset;
use yii\helpers\Html;
use kartik\slider\Slider;
use kartik\switchinput\SwitchInput;
use kartik\widgets\FileInput;
use yii\helpers\Url;

BootstrapPluginAsset::register($this);

$labelOptions = ['class' => 'control-label'];
$inputOptions = ['class' => 'form-control'];
//单选radio|select,复选checkbox,区间between,文字text,图片image,文件file
?>
<div class="form-group">
    <div class="col-sm-12">
        <div class="col-sm-2">
            <?= Html::label('赛事是否编辑', null, $labelOptions) ?>
        </div>
        <div class="col-sm-2 col-sm-offset-8">
            <?= Html::label('赛事是否显示', null, $labelOptions) ?>
        </div>
    </div>
</div>
<?php foreach ($details as $key => $value): ?>
    <div class="form-group par-game-conf">
        <div class="col-sm-12" style="margin-bottom: 10px">
            <?= Html::hiddenInput("GameConf[{$key}][meta_key]", $value['meta_key'], $inputOptions) ?>
            <?= Html::hiddenInput("GameConf[{$key}][meta_name]", $value['meta_name'], $inputOptions) ?>
            <?= Html::hiddenInput("GameConf[{$key}][type]", $value['type'], $inputOptions); ?>
            <div class="col-sm-2">
                <?= SwitchInput::widget([
                    'name' => "GameConf[{$key}][is_write]",
                    'value' => $value['model_is_write'],
//                    'pluginOptions' => [
//                        'size' => 'small',
//                    ]
                ]) ?>
            </div>

            <div class="col-sm-8">
                <div class="input-group meta-value">
                    <?= Html::tag('span', $value['meta_name'], ['class' => 'input-group-addon']) ?>
                    <?php switch ($value['type']): case 'radio': ?>
                        <?= Html::radioList("GameConf[{$key}][meta_value]", $value['format_model_meta_value'], $value['format_meta_value'], $inputOptions); ?>
                        <?php break; ?>
                    <?php case 'select': ?>
                        <?= Html::checkboxList("GameConf[{$key}][meta_value]", $value['format_model_meta_value'], $value['format_meta_value'], $inputOptions); ?>
                        <?php break; ?>
                    <?php case 'checkbox': ?>
                        <?= Html::checkboxList("GameConf[{$key}][meta_value]", $value['format_model_meta_value'], $value['format_meta_value'], $inputOptions); ?>
                        <?php break; ?>
                    <?php case 'between': ?>
                        <div class="form-control">
                            <b class="badge"><?= intval(reset($value['format_meta_value'])) ?></b>
                            &nbsp;
                            <?= Slider::widget([
                                'name' => "GameConf[{$key}][meta_value]",
                                'value' => $value['model_meta_value'] ? $value['model_meta_value'] : $value['meta_value'],
                                'sliderColor' => Slider::TYPE_INFO,
                                'pluginOptions' => [
                                    'min' => intval(reset($value['format_meta_value'])),
                                    'max' => intval(end($value['format_meta_value'])),
                                    'step' => 1,
                                    'range' => true
                                ],
                            ]); ?>
                            &nbsp;
                            <b class="badge"><?= intval(end($value['format_meta_value'])) ?></b>
                        </div>
                        <?php break; ?>
                    <?php case 'text': ?>
                        <?= Html::textInput("GameConf[{$key}][meta_value]", $value['format_meta_value'], $inputOptions); ?>
                        <?php break; ?>
                    <?php case 'image': ?>

                        <?= FileInput::widget([
                            'model' => $model,
                            'attribute' => "file[{$key}]",
                            'options' => [
                                'accept' => 'image/*',
                                'multiple' => true
                            ],
                            'pluginOptions' => [
                                'previewFileType' => 'image',
                                'uploadUrl' => Url::to(['upload-file'], true),
                                'maxFileCount' => 1,
                                'initialPreview' => $value['format_model_meta_value'] ? [Html::img($value['format_model_meta_value'], ['width' => 'auto', 'height' => 'auto', 'style' => 'max-width:100%;max-height:100%;'])] : '',
                                'uploadExtraData' => [
                                    'dir' => strtolower($model->formName()),
                                    'file' => "file[{$key}]",
                                ],
                                'uploadAsync' => true,
                            ],
                            'pluginEvents' => [
                                "fileuploaded" => "function (event, data, id, index) {
                                    console.log(data);
                                    $(\"input[name='GameConf[{$key}][meta_value]']\").val(data.response.data.link);
                                }",
                            ],
                        ]); ?>

                        <?= Html::hiddenInput("GameConf[{$key}][meta_value]", $value['format_model_meta_value'], $inputOptions); ?>
                        <?php break; ?>
                    <?php case 'file': ?>

                        <?= FileInput::widget([
                            'model' => $model,
                            'attribute' => "file[{$key}]",
                            'options' => [
                                'accept' => '*',
                                'multiple' => true
                            ],
                            'pluginOptions' => [
                                'showPreview' => false,
                                'uploadUrl' => Url::to(['upload-file'], true),
                                'maxFileCount' => 1,
                                'initialPreview' => $value['format_model_meta_value'] ? [Html::img($value['format_model_meta_value'], ['width' => 'auto', 'height' => 'auto', 'style' => 'max-width:100%;max-height:100%;'])] : '',
                                'uploadExtraData' => [
                                    'dir' => strtolower($model->formName()),
                                    'file' => "file[{$key}]",
                                ],
                                'uploadAsync' => true,
                            ],
                            'pluginEvents' => [
                                "fileuploaded" => "function (event, data, id, index) {
                                    console.log(data);
                                    $(\"input[name='GameConf[{$key}][meta_value]']\").val(data.response.data.link);
                                }",
                            ],
                        ]); ?>

                        <?= Html::hiddenInput("GameConf[{$key}][meta_value]", $value['format_model_meta_value'], $inputOptions); ?>
                        <?php break; ?>
                    <?php default: ?>
                    <?php endswitch; ?>
                </div>
            </div>

            <div class="col-sm-2">
                <?= SwitchInput::widget([
                    'name' => "GameConf[{$key}][is_show]",
                    'value' => $value['model_is_show'],
                ]) ?>
            </div>
        </div>
    </div>
<?php endforeach; ?>


<?php $this->beginBlock('file') ?>

$(function($) {

$(document).on('click', '.file-input .kv-file-remove', function () {
$(this).parents('.file-input').find('.fileinput-remove').click();
});

$(document).on('click', '.fileinput-remove', function () {
$(this).parents('.meta-value').find('input').val(null);
});

});

<?php $this->endBlock() ?>
<?php $this->registerJs($this->blocks['file'], \yii\web\View::POS_END); ?>
